package java44th.group.service.admin.issue;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.admin.bo.issue.IssueCreateBo;
import java44th.group.bean.admin.bo.issue.IssueUpdteBo;
import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketIssue;
import java44th.group.bean.po.MarketIssueExample;
import java44th.group.mapper.MarketIssueMapper;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author 小焦
 * @since 2022/10/12 18:01
 */

@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    MarketIssueMapper issueMapper;
    /**
     *  获取所有问题记录
     * @param info
     * @param question
     * @return java44th.group.bean.common.CommonData<java44th.group.bean.po.MarketIssue>
     * @author 小焦
     * @since 2022/10/13 上午 11:55
     */
    @Override
    public CommonData<MarketIssue> list(BasePageInfo info,String question) {
        PageHelper.startPage(info.getPage(),info.getLimit());

        MarketIssueExample example = new MarketIssueExample();
        example.setOrderByClause(info.getSort()+" "+info.getOrder());
        MarketIssueExample.Criteria criteria = example.createCriteria();
        List<MarketIssue> list;
        if (!StringUtil.isEmpty(question)) {
            criteria.andQuestionLike("%"+question+"%");
        }
        criteria.andDeletedEqualTo(false);
        list = issueMapper.selectByExample(example);
        PageInfo<MarketIssue> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    /**
     *  创建一条问题记录
     * @param issueCreateBo
     * @return java44th.group.bean.po.MarketIssue
     * @author 小焦
     * @since 2022/10/13 上午 11:44
     */
    @Override
    public MarketIssue create(IssueCreateBo issueCreateBo) {
        MarketIssue issue = new MarketIssue();
        issue.setQuestion(issueCreateBo.getQuestion());
        issue.setAnswer(issueCreateBo.getAnswer());
        issue.setAddTime(new Timestamp(System.currentTimeMillis()));
        issue.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        issue.setDeleted(false);
        int count = issueMapper.insert(issue);
        MarketIssue marketIssue = issueMapper.selectByPrimaryKey(issue.getId());
        return marketIssue;
    }


    /**
     *  更新一条问题记录
     * @param issueUpdteBo
     * @return java44th.group.bean.po.MarketIssue
     * @author 小焦
     * @since 2022/10/13 上午 11:55
     */
    @Override
    public MarketIssue update(IssueUpdteBo issueUpdteBo) {
        MarketIssue issue = new MarketIssue();
        issue.setQuestion(issueUpdteBo.getQuestion());
        issue.setAnswer(issueUpdteBo.getAnswer());
        issue.setId(issueUpdteBo.getId());
        issue.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        int count = issueMapper.updateByPrimaryKeySelective(issue);
        MarketIssue marketIssue = issueMapper.selectByPrimaryKey(issue.getId());

        return marketIssue;
    }

    /**
     *  逻辑删除一条问题记录
     * @param issueUpdteBo
     * @return int
     * @author 小焦
     * @since 2022/10/13 上午 11:54
     */
    @Override
    public int delete(IssueUpdteBo issueUpdteBo) {
        MarketIssue issue = new MarketIssue();
        issue.setId(issueUpdteBo.getId());
        issue.setDeleted(true);
        issue.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        int count = issueMapper.updateByPrimaryKeySelective(issue);
        if(count==1){
            return count;
        }
        return 0;
    }
}
